package com.tenement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tenement.entity.Resolution;
import com.tenement.mapper.ResolutionMapper;
import com.tenement.service.ResolutionService;
import com.tenement.service.VoteService;
import com.tenement.vo.QueryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.tenement.entity.table.ResolutionTableDef.RESOLUTION;
import static com.tenement.entity.table.UserTableDef.USER;

/**
 *  服务层实现。
 *
 * @author BertXie
 * @since 1.0.1
 */
@Service
@RequiredArgsConstructor
public class ResolutionServiceImpl extends ServiceImpl<ResolutionMapper, Resolution> implements ResolutionService {

    private final ResolutionMapper resolutionMapper;


    @Autowired
    @Lazy
    private VoteService voteService;

    @Override
    public Page<Resolution> list(QueryVo queryVo) {
        UpdateChain.of(Resolution.class)
                .set(RESOLUTION.STATUS,1)
                .where(RESOLUTION.START_TIME.le(new Timestamp(new Date().getTime()))).update();
        UpdateChain.of(Resolution.class)
                .set(RESOLUTION.STATUS,2)
                .where(RESOLUTION.END_TIME.le(new Timestamp(new Date().getTime()))).update();
        QueryWrapper queryWrapper = QueryWrapper.create().from(RESOLUTION)
                .select(
                        RESOLUTION.ID, RESOLUTION.TITLE, RESOLUTION.COVER,
                        RESOLUTION.TYPE, RESOLUTION.MULTIPLE, RESOLUTION.DEC,
                        RESOLUTION.SHOW_WAY, RESOLUTION.END_TIME, RESOLUTION.DESCRIPTION,
                        RESOLUTION.UPDATE_TIME, RESOLUTION.OPTIONS, RESOLUTION.START_TIME,RESOLUTION.IS_ANONYMITY,
                        RESOLUTION.AT_MOST, RESOLUTION.CREATE_TIME,RESOLUTION.STATUS,
                        RESOLUTION.VOTE_TYPE, RESOLUTION.PUSH_ID,
                        USER.REAL_NAME, USER.ID, USER.PHONE_NUMBER
                ).leftJoin(USER).on(RESOLUTION.PUSH_ID.eq(USER.ID))
                .where(
                        RESOLUTION.DESCRIPTION.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord()))
                                .or(RESOLUTION.TITLE.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(RESOLUTION.OPTIONS.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(USER.REAL_NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                )
                .and(RESOLUTION.TYPE.eq(queryVo.getType(), queryVo.getType() != -1))
                .and(RESOLUTION.VOTE_TYPE.eq(queryVo.getStatus(), queryVo.getStatus() != -1))
                .between("start_time", queryVo.getTimeRange() == null ? "" : queryVo.getTimeRange().get(0), queryVo.getTimeRange() == null ? "" : queryVo.getTimeRange().get(1), queryVo.getTimeRange() != null && queryVo.getTimeRange().size() == 2)
                .orderBy(RESOLUTION.END_TIME.desc());
        return resolutionMapper.paginate(queryVo.getPage(), queryVo.getSize(), queryWrapper);
    }

    @Transactional
    @Override
    public int remove(List<Long> ids) {
        voteService.removeByResolutionIds(ids);
        return resolutionMapper.deleteBatchByIds(ids);
    }

    @Override
    public Resolution usable(Long resolutionId) {
        return resolutionMapper.selectOneByQuery(QueryWrapper.create().from(RESOLUTION).where(RESOLUTION.ID.eq(resolutionId)).and(RESOLUTION.STATUS.eq(1)));
    }
}
